package com.momo.hotel.interceptor;

import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.vo.VoUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @version 1.0.0
 * @auther Snake
 * @date 2019/7/2 17:46
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        VoUser user = (VoUser) httpServletRequest.getSession().getAttribute("user");

        String uri = httpServletRequest.getRequestURI();

        // 判断用户是否登录
        if (user != null ||uri.indexOf("userLogin")>0||uri.endsWith("register1.html")||uri.endsWith("login1.html")||uri.indexOf("checkUserName")>0||uri.indexOf("userRegister")>0||
                uri.indexOf("static")>0||uri.endsWith("index.html")||uri.endsWith("header.html")||uri.endsWith("footer.html")) {
            // 用户未登录，重定向到登录页
//            httpServletResponse.sendRedirect("/login1");
            httpServletResponse.getWriter().write("<a href='login1.html'>");
            return true;
        }

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
